<?php

class DashBoardController extends PsController {

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
						//'roles' => array('Transactions.ViewOwn'),
					)), parent::accessRules());
	}
	
	public function actionView(){
	
		$ticketType = TicketType::model()->findAllByAttributes(array('agency_id' => Yii::app()->user->model->agency_id));
		//dump($ticketType);exit;
		
		$dataa = array();
		//exit;
		// Search by Two date
		if (isset($_GET['dateBegin']) && isset($_GET['dateEnd'])){
			$date_begin = date('Y-m-d H:i:s',strtotime($_GET['dateBegin']));
			$date_end = date('Y-m-d H:i:s', strtotime($_GET['dateEnd'])+ 86400);
			//echo $date_begin.'<br/>';
			//echo $date_end.'<br/>';
			
			//total Open
			$criteria1 = new CDbCriteria;
			
			$criteria1->together = true;
			$criteria1->with=array('ticket');
			$criteria1->compare('ticket.agency_id' , Yii::app()->user->model->agency_id , true);
			$criteria1->compare('t.ticket_status_type_id' , 1 , true);
			$criteria1->addBetweenCondition('t.update_time', $date_begin, $date_end);
			$dataa[0] = count(TicketStatusHistory::model()->findAll($criteria1));
			
			// total Close
			$criteria2 = new CDbCriteria;
			
			$criteria2->together = true;
			$criteria2->with=array('ticket');
			$criteria2->compare('ticket.agency_id' , Yii::app()->user->model->agency_id , true);
			$criteria2->compare('t.ticket_status_type_id' , 2 , true);
			$criteria2->addBetweenCondition('t.update_time', $date_begin, $date_end);
			$dataa[1] = count(TicketStatusHistory::model()->findAll($criteria2));
			
			//Total Ticket
			$criteria3 = new CDbCriteria;
			
			$criteria3->compare('t.agency_id' , Yii::app()->user->model->agency_id , true);
			$criteria3->addBetweenCondition('t.create_time', $date_begin, $date_end);
			$countticket = count (Ticket::model()->findAll($criteria3));
		}
		else{
			$dataa[0] = count(TicketStatusHistory::model()->findAllByAttributes(array('ticket_status_type_id' => 1)));
			$dataa[1] = count(TicketStatusHistory::model()->findAllByAttributes(array('ticket_status_type_id' => 2)));
			$countticket = count (Ticket::model()->findAll());
		}
		
		//dump($countticket);exit;
		$numberReopen = $dataa[0] - $countticket;
		$tmp = array(
			array(
				"value" => $countticket,
				"color" => "green",
				"label" => "Open"
			),
			array(
				"value" => $dataa[1],
				"color" => "red",
				"label" => "Close"
			),
			array(
				"value" => $numberReopen,
				"color" => "yellow",
				"label" => "Reopen"
			),
			array(
				"value" => $countticket + $numberReopen - $dataa[1],
				"color" => "orange",
				"label" => "Overdue"
			),
		);
		$this->render('view',array(
		'ticketType' => $ticketType,
		'tmp'=>$tmp,
		'dataa' => $dataa,
		'numberReopen' => $numberReopen,
		'countticket' =>  $countticket,
		));
	}
	
}